<?php

class Browser {

    private $name;
    private $version;
    private $platform;
    private $userAgent;
    private $moreInfo;
    private $javascriptEnabled;
    private $cookiesEnabled;
    private $ip;

    /**
      Figure out what browser is used, its version and the platform it is
      running on.

      The following code was ported in part from JQuery v1.3.1
     */
    public function __construct() {
        $this->userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);

        $supportedBrowsers = array('firefox' => 'Mozilla Firefox',
            'msie' => 'Microsoft Internet Explorer',
            'opera' => 'Opera',
            'chrome' => 'Google Chrome',
            'safari' => 'Apple Safari',
            'mozilla' => 'Mozilla',
            'seamonkey' => 'Seamonkey',
            'konqueror' => 'Konqueror',
            'netscape' => 'Netscape',
            'gecko' => 'Gecko',
            'navigator' => 'Navigator',
            'mosaic' => 'Mosaic',
            'lynx' => 'Lynx',
            'amaya' => 'Amaya',
            'omniweb' => 'Omniweb',
            'avant' => 'Avant',
            'camino' => 'Camino',
            'flock' => 'Flock',
            'aol' => 'AOL');

        foreach ($supportedBrowsers as $browserTemp => $item) {
            if (preg_match("#($browserTemp)[/ ]?([0-9.]*)#", $this->userAgent, $match)) {
                $this->name = $supportedBrowsers[$match[1]];
                $this->version = $match[2];
                break;
            }
        }

        if (empty($this->name)) {
            $this->name = 'Unknow';
        }

        // Running on what platform?
        if (preg_match('/linux/', $this->userAgent)) {
            $this->platform = 'Linux';
        } elseif (preg_match('/macintosh|mac os x/', $this->userAgent)) {
            $this->platform = 'Mac';
        } elseif (preg_match('/windows|win32/', $this->userAgent)) {
            $this->platform = 'Windows';
        } else {
            $this->platform = 'Unknow';
        }

        /* Retrieving IP */
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {//check ip from share internet
            $this->ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {//to check ip is pass from proxy
            $this->ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $this->ip = $_SERVER['REMOTE_ADDR'];
        }

        //$this->moreInfo = get_browser(null, true);
        $this->javascriptEnabled = $this->moreInfo['javascript'];
        $this->cookiesEnabled = $this->moreInfo['cookies'];
    }

    public function getName() {
        return $this->name;
    }

    public function getVersion() {
        return $this->version;
    }

    public function getPlatform() {
        return $this->platform;
    }

    public function getUserAgent() {
        return $this->userAgent;
    }

    public function isJavascriptEnabled() {
        return $this->javascriptEnabled;
    }

    public function isCookiesEnabled() {
        return $this->cookiesEnabled;
    }

    public function getMoreInfo() {
        return $this->moreInfo;
    }

    public function getIp() {
        return $this->ip;
    }

}
